import 'package:flutter/material.dart';

import '../../flutter_widgets.dart';

class CustomButtonStyle {
  final FlatButtonStyle flatButtonStyle;
  final OutlinedButtonStyle outlinedButtonStyle;
  final RaisedButtonStyle raisedButtonStyle;

  const CustomButtonStyle({
    this.flatButtonStyle = const FlatButtonStyle(),
    this.outlinedButtonStyle = const OutlinedButtonStyle(),
    this.raisedButtonStyle = const RaisedButtonStyle(),
  });
}

class FlatButtonStyle {
  final Color? highlightColor;
  final Color? disabledTextColor;
  final TextStyle? textStyle;

  const FlatButtonStyle({
    this.highlightColor,
    this.disabledTextColor,
    this.textStyle,
  });

  FlatButtonStyle copyWith({
    Color? highlightColor,
    Color? disabledTextColor,
    TextStyle? textStyle,
  }) {
    return FlatButtonStyle(
      highlightColor: highlightColor ?? this.highlightColor,
      disabledTextColor: disabledTextColor ?? this.disabledTextColor,
      textStyle: textStyle ?? this.textStyle,
    );
  }
}

class CommonFlatButton extends StatelessWidget {
  final String text;
  final FlatButtonStyle? style;
  final VoidCallback? onPressed;

  const CommonFlatButton({
    super.key,
    required this.text,
    this.onPressed,
    this.style,
  });

  @override
  Widget build(BuildContext context) {
    final style =
        this.style ?? (WidgetTheme.of(context)?.buttonStyle.flatButtonStyle);
    var textStyle = style?.textStyle ?? Theme.of(context).textTheme.labelLarge;
    if (onPressed == null) {
      textStyle = textStyle?.copyWith(
        color: style?.disabledTextColor ?? Theme.of(context).disabledColor,
      );
    }
    return TextButton(
      style: TextButton.styleFrom(
        backgroundColor: Colors.transparent,
        foregroundColor: style?.highlightColor,
      ),
      onPressed: onPressed,
      child: Text(
        text,
        style: textStyle,
      ),
    );
  }
}

class OutlinedButtonStyle {
  final double? width;
  final double? height;
  final Color? highlightColor;
  final Color? disabledTextColor;
  final Color? borderColor;
  final double? borderWidth;
  final TextStyle? textStyle;

  const OutlinedButtonStyle({
    this.width,
    this.height,
    this.highlightColor,
    this.disabledTextColor,
    this.borderColor,
    this.borderWidth,
    this.textStyle,
  });

  OutlinedButtonStyle copyWith({
    double? width,
    double? height,
    Color? highlightColor,
    Color? disabledTextColor,
    Color? borderColor,
    double? borderWidth,
    TextStyle? textStyle,
  }) {
    return OutlinedButtonStyle(
      width: width ?? this.width,
      height: height ?? this.height,
      highlightColor: highlightColor ?? this.highlightColor,
      disabledTextColor: disabledTextColor ?? this.disabledTextColor,
      borderColor: borderColor ?? this.borderColor,
      borderWidth: borderWidth ?? this.borderWidth,
      textStyle: textStyle ?? this.textStyle,
    );
  }
}

class CommonOutlinedButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final OutlinedButtonStyle? style;

  const CommonOutlinedButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.style,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final style =
        this.style ?? WidgetTheme.of(context)?.buttonStyle.outlinedButtonStyle;
    var textStyle = style?.textStyle ?? Theme.of(context).textTheme.labelLarge;
    if (onPressed == null) {
      textStyle = textStyle?.copyWith(
          color: style?.disabledTextColor ?? Theme.of(context).disabledColor);
    }
    return ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: style?.width ?? 250,
        minHeight: style?.height ?? 48,
      ),
      child: OutlinedButton(
        onPressed: onPressed,
        style: ButtonStyle(
            overlayColor: MaterialStateProperty.resolveWith((states) {
              if (states.contains(MaterialState.pressed) ||
                  states.contains(MaterialState.selected) ||
                  states.contains(MaterialState.focused)) {
                return style?.highlightColor;
              } else {
                return Colors.transparent;
              }
            }),
            side: MaterialStateProperty.all(BorderSide(
                color: style?.borderColor ?? Theme.of(context).primaryColor,
                width: style?.borderWidth ?? 2))),
        child: Text(
          text,
          style: textStyle,
        ),
        // splashColor: style.splashColor,
      ),
    );
  }
}

class RaisedButtonStyle {
  final TextStyle? textStyle;
  final double? elevation;
  final double? highlightElevation;
  final double? disabledElevation;
  final double? width;
  final double? height;
  final Color? color;
  final Color? highlightColor;
  final Color? disabledTextColor;

  const RaisedButtonStyle({
    this.textStyle,
    this.elevation,
    this.highlightElevation,
    this.disabledElevation,
    this.width,
    this.height,
    this.color,
    this.highlightColor,
    this.disabledTextColor,
  });

  RaisedButtonStyle copyWith({
    TextStyle? textStyle,
    double? elevation,
    double? highlightElevation,
    double? disabledElevation,
    double? width,
    double? height,
    Color? color,
    Color? highlightColor,
    Color? disabledTextColor,
  }) {
    return RaisedButtonStyle(
      textStyle: textStyle ?? this.textStyle,
      elevation: elevation ?? this.elevation,
      highlightElevation: highlightElevation ?? this.highlightElevation,
      disabledElevation: disabledElevation ?? this.disabledElevation,
      width: width ?? this.width,
      height: height ?? this.height,
      color: color ?? this.color,
      highlightColor: highlightColor ?? this.highlightColor,
      disabledTextColor: disabledTextColor ?? this.disabledTextColor,
    );
  }
}

class CommonRaisedButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final RaisedButtonStyle? style;

  const CommonRaisedButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.style,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final style =
        this.style ?? WidgetTheme.of(context)?.buttonStyle.raisedButtonStyle;
    var textStyle = style?.textStyle ?? Theme.of(context).textTheme.labelLarge;
    if (onPressed == null && style?.disabledTextColor != null) {
      textStyle = textStyle?.copyWith(color: style?.disabledTextColor);
    }
    return ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: style?.width ?? 250,
        minHeight: style?.height ?? 48,
      ),
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: style?.color,
          foregroundColor: style?.highlightColor,
          disabledForegroundColor: style?.color,
        ).copyWith(elevation: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.focused)) {
            return style?.highlightElevation;
          } else if (states.contains(MaterialState.disabled)) {
            return style?.disabledElevation;
          } else {
            return style?.elevation;
          }
        })),
        child: Text(
          text,
          style: textStyle,
        ),
      ),
    );
  }
}
